import api from './auth'

// 获取所有用户
export const getAllUsers = async () => {
  try {
    const response = await api.get('/api/admin/users')
    return response.data
  } catch (error) {
    throw new Error(error.response?.data?.message || '获取用户列表失败')
  }
}

// 获取指定角色用户
export const getUsersByRole = async (role) => {
  try {
    const response = await api.get(`/api/admin/users/role/${role}`)
    return response.data
  } catch (error) {
    throw new Error(error.response?.data?.message || '获取用户列表失败')
  }
}

// 更新用户角色
export const updateUserRole = async (userId, role) => {
  try {
    const response = await api.put(`/api/admin/users/${userId}/role`, { role })
    return response.data
  } catch (error) {
    throw new Error(error.response?.data?.message || '更新用户角色失败')
  }
}

// 删除用户
export const deleteUser = async (userId) => {
  try {
    const response = await api.delete(`/api/admin/users/${userId}`)
    return response.data
  } catch (error) {
    throw new Error(error.response?.data?.message || '删除用户失败')
  }
}

// 获取所有历史记录
export const getAllHistory = async (page = 1, pageSize = 10) => {
  try {
    const response = await api.get(`/api/admin/history?page=${page}&pageSize=${pageSize}`)
    return response.data
  } catch (error) {
    throw new Error(error.response?.data?.message || '获取历史记录失败')
  }
}

// 获取所有SQL执行记录
export const getAllExecutions = async (limit = 100) => {
  try {
    const response = await api.get(`/api/admin/executions?limit=${limit}`)
    return response.data
  } catch (error) {
    throw new Error(error.response?.data?.message || '获取SQL执行记录失败')
  }
}

// 获取SQL执行统计信息
export const getExecutionStatistics = async () => {
  try {
    const response = await api.get('/api/admin/executions/statistics')
    return response.data
  } catch (error) {
    throw new Error(error.response?.data?.message || '获取SQL执行统计信息失败')
  }
}

// 获取系统统计信息
export const getStatistics = async () => {
  try {
    const response = await api.get('/api/admin/statistics')
    return response.data
  } catch (error) {
    throw new Error(error.response?.data?.message || '获取统计信息失败')
  }
}

// 获取所有用户的数据库连接
export const getAllDbConnections = async () => {
  try {
    const response = await api.get('/api/admin/db-connections')
    return response.data
  } catch (error) {
    throw new Error(error.response?.data?.message || '获取数据库连接列表失败')
  }
}

// 获取指定用户的数据库连接
export const getUserDbConnections = async (userId) => {
  try {
    const response = await api.get(`/api/admin/users/${userId}/db-connections`)
    return response.data
  } catch (error) {
    throw new Error(error.response?.data?.message || '获取用户数据库连接失败')
  }
}

// 测试指定的数据库连接
export const testDbConnection = async (connectionId) => {
  try {
    const response = await api.post(`/api/admin/db-connections/${connectionId}/test`)
    return response.data
  } catch (error) {
    throw new Error(error.response?.data?.message || '测试连接失败')
  }
}

// 禁用/启用数据库连接
export const toggleDbConnectionStatus = async (connectionId, isEnabled) => {
  try {
    const response = await api.put(`/api/admin/db-connections/${connectionId}/status`, { isEnabled })
    return response.data
  } catch (error) {
    throw new Error(error.response?.data?.message || '更新连接状态失败')
  }
}

// 删除数据库连接
export const deleteDbConnection = async (connectionId) => {
  try {
    const response = await api.delete(`/api/admin/db-connections/${connectionId}`)
    return response.data
  } catch (error) {
    throw new Error(error.response?.data?.message || '删除数据库连接失败')
  }
}

// 刷新所有元数据缓存
export const refreshAllMetadata = async () => {
  try {
    const response = await api.post('/api/admin/metadata/refresh-all')
    return response.data
  } catch (error) {
    throw new Error(error.response?.data?.message || '刷新元数据缓存失败')
  }
}

// 清理所有元数据缓存
export const cleanAllMetadata = async () => {
  try {
    const response = await api.post('/api/admin/metadata/clean-all')
    return response.data
  } catch (error) {
    throw new Error(error.response?.data?.message || '清理元数据缓存失败')
  }
} 